package org.mii.safeguard_v1.tools.itext;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import static org.mii.safeguard_v1.tools.itext.FontFactory.*;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Header;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;

public class TestPDFService {
	public static void print(HttpServletResponse response) throws Exception {
		Document document = new Document(PageSize.A4, 72, 72, 54, 72);
		response.setContentType("application/pdf");
		response.setCharacterEncoding("UTF-8");
		try {
			response.setHeader("Content-Disposition", "attachment; filename="
					+ new String("测试文档.pdf".getBytes("GBK"), "iso-8859-1"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Font font_hd = createFontSong(9, Font.NORMAL);
		Font font_ft = createFontSong(9, Font.NORMAL);

		try {
			PdfWriter writer = PdfWriter.getInstance(document, response
					.getOutputStream());

			Phrase headpar = new Phrase("哥写的不是程序，是寂寞", font_hd);
			HeaderFooter header = new HeaderFooter(headpar, false);
			header.setBorder(0);
			header.setAlignment(Header.ALIGN_RIGHT);
			header.setBorderWidthBottom(0.5f);
			document.setHeader(header);

			HeaderFooter footer = new HeaderFooter(new Phrase(
					"北京科技大学 管理科学与工程系", font_ft), true);
			footer.setBorder(0);
			footer.setBorderWidthTop(0.5f);
			document.setFooter(footer);

			document.open();

			Paragraph line1 = new Paragraph("本行字是黑体，你看见了吗？", createFontHei(16,
					Font.NORMAL));
			line1.setAlignment(Paragraph.ALIGN_CENTER);
			document.add(line1);
			Paragraph line2 = new Paragraph("本行字是宋体，你看见了吗？", createFontSong(16,
					Font.NORMAL));
			line2.setAlignment(Paragraph.ALIGN_CENTER);
			document.add(line2);
			Paragraph line3 = new Paragraph("本行字是斜体，你看见了吗？", createFontSong(16,
					Font.ITALIC));
			line3.setAlignment(Paragraph.ALIGN_CENTER);
			document.add(line3);
			Paragraph line4 = new Paragraph("本行字是粗体，你看见了吗？", createFontSong(16,
					Font.BOLD));
			line4.setAlignment(Paragraph.ALIGN_CENTER);
			document.add(line4);
			Paragraph line5 = new Paragraph("本行字被删掉了，你看见了吗？", createFontSong(
					16, Font.STRIKETHRU));
			line5.setAlignment(Paragraph.ALIGN_CENTER);
			document.add(line5);
			Paragraph line6 = new Paragraph("本行字有下划线，你看见了吗？", createFontSong(
					16, Font.UNDERLINE));
			line6.setAlignment(Paragraph.ALIGN_CENTER);
			document.add(line6);

		} catch (Exception de) {
			de.printStackTrace();
			throw de;
		}
		document.close();
	}
}
